from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app.models.user import User
from app.core.config import settings

# 创建数据库连接
engine = create_engine(settings.DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
db = SessionLocal()

try:
    # 查找所有 status 为 None 的用户
    users = db.query(User).filter(User.status == None).all()
    
    if users:
        count = 0
        for user in users:
            user.status = "active"
            count += 1
        
        db.commit()
        print(f"已更新 {count} 个用户的状态为 'active'")
    else:
        print("没有找到 status 为 None 的用户")
        
except Exception as e:
    print(f"发生错误: {e}")
    db.rollback()
finally:
    db.close() 